package com.lagou.rpc.provider;

import com.lagou.rpc.provider.server.RpcServer;
import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ServerBootstrapApplication implements CommandLineRunner {

    @Autowired
    RpcServer rpcServer;
   static ZkClient zkClient =null;
    public static void main(String[] args) {
        zkClient =new ZkClient("106.75.147.53:2181");
         //判断是否存在
        SpringApplication.run(ServerBootstrapApplication.class, args);
    }


    @Override
    public void run(String... args) throws Exception {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (!zkClient.exists("/lg-service")) {
                    zkClient.create("/lg-service", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                }
                zkClient.createEphemeral( "/lg-service/server-" + "9999","127.0.0.1:9999");
                rpcServer.startServer("127.0.0.1", 9999);


            }
        }).start();
    }



}
